package com.sz.controller;

import com.sz.model.OnlineBank;
import com.sz.model.OnlineBankCard;
import com.sz.model.Result;
import com.sz.myinterface.IdentityCard;
import com.sz.service.IOnlineBankCardService;
import com.sz.service.IOnlineBankService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/ob")
public class OnlineBankController {

    @Autowired
    private IOnlineBankService iOnlineBankService;

    @Autowired
    private IOnlineBankCardService iOnlineBankCardService;

    @RequestMapping("/login")
    public Result login(@RequestBody OnlineBank onlineBank){

        System.out.println("onlineBank.toString() = " + onlineBank.toString());
        Result<OnlineBank> result=null;
        OnlineBank user = iOnlineBankService.getUser(onlineBank.getObUname(), onlineBank.getObUpwd());

        if(user!=null){
            result=new Result(200,"登录成功",user);

        }else{
            result=new Result(301,"登录失败");
        }

        return result;
    }

    //获取银行卡list
    @RequestMapping(value = "/list",method = RequestMethod.POST)
    public Result getUser(@RequestBody int obid){
        System.out.println("obid = " + obid);
        Result result=null;

        if(obid!=0){
            List<OnlineBankCard> allBankCard = iOnlineBankCardService.getAllBankCard(obid);
            OnlineBank oneUser = iOnlineBankService.getUserById(obid);

            Map<OnlineBank,List<OnlineBankCard>> map=new HashMap<>();
            map.put(oneUser,allBankCard);

            result=new Result<>(200,"成功",oneUser);
        }else{
            result=new Result<>(301,"失败");
        }

        return result;
    }

    //查询身份证
    @RequestMapping(value = "/idcard",method = RequestMethod.POST)
    public Result getIdCard(String ic,String name){
        System.out.println("name = " + name);
        Result result=null;
        //查询数据库，IdCard，name
        OnlineBank userByIdCard = iOnlineBankService.getUserByIdCard(ic, name);

        if(userByIdCard!=null){
            //接口查询
            String userIC = IdentityCard.getUserIC(ic, name);
            System.out.println("userIC = " + userIC);
            result=new Result(200,"成功",userIC);
        }else{
            result=new Result(303,"用户不存在");
        }

        return result;
    }
}
